import arcpy

import os

def get_script_path():
    # Get the absolute path of the currently running script
    return os.path.abspath(__file__)

wkdir = os.path.dirname(get_script_path())

# fishnet
fishnet = r'%s\local\gis_data\003_boundaries\fishnet_lake_chad_adm2_x_d01.shp' % wkdir

# roads
roads = r'%s\local\gis_data\018_transportation\roads_lake_chad.shp' % wkdir
roadsdiss = r'%s\local\gis_data\018_transportation\roads_lake_chad_diss.shp' % wkdir
intersectRoads = r'%s\local\gis_data\018_transportation\roads_lake_chad_pt.shp' % wkdir
roads2008 = r'%s\local\gis_data\018_transportation\roads_lake_chad_r2008.shp' % wkdir

# output
fishnetIntersectRoads = r'%s\proc_data\roads_intersection_pt.shp' % wkdir
fishnetRoadLength = r'%s\proc_data\roads_length.shp' % wkdir

##
## Road intersections
##

## merge road lines of same R2008
#dissolveField = "R2008"
dissolveField = "d_diss"
fieldnames = [f.name for f in arcpy.ListFields(roads)]
if dissolveField not in fieldnames:
    arcpy.AddField_management(roads, dissolveField, "SHORT", 9)
    
road_lyr = "rd_lyr"
arcpy.MakeFeatureLayer_management(roads, road_lyr)

#arcpy.SelectLayerByAttribute_management(road_lyr, "NEW_SELECTION", expression)
# Execute Dissolve using Dissolve Field
if not arcpy.Exists(roadsdiss):
    arcpy.Dissolve_management(road_lyr, roadsdiss, dissolveField, "", 
                          "MULTI_PART", "DISSOLVE_LINES")

## road lines to points
if not arcpy.Exists(intersectRoads):
    arcpy.Intersect_analysis(roadsdiss, intersectRoads, "ALL", "", "POINT")

# multiple points at 1 intersection :: MUST DISSOLVE AGAIN!
intersectRoadsDiss = "in_memory\\intersectRdDiss"
if not arcpy.Exists(intersectRoadsDiss):
    #arcpy.AddField_management(intersectRoads, dissolveField, "SHORT", 9)
    arcpy.Dissolve_management(intersectRoads, intersectRoadsDiss, dissolveField, "", "MULTI_PART", "")

# multi to singlepart
intersectRoadsSng = "in_memory\\intersectRdSng"
if not arcpy.Exists(intersectRoadsSng):
    arcpy.MultipartToSinglepart_management(intersectRoadsDiss,intersectRoadsSng)

# extract by fishnet
arcpy.Delete_management(fishnetIntersectRoads)
if not arcpy.Exists(fishnetIntersectRoads):
    arcpy.SpatialJoin_analysis(intersectRoadsSng, fishnet, fishnetIntersectRoads)

##
## Road length... rays by quality
##

if not arcpy.Exists(roads2008):
    arcpy.Dissolve_management(road_lyr, roads2008, "R2008", "", 
                          "SINGLE_PART", "DISSOLVE_LINES")
    
arcpy.Delete_management(fishnetRoadLength)
if not arcpy.Exists(fishnetRoadLength):
    arcpy.Identity_analysis(roads2008, fishnet, fishnetRoadLength)

## calculate road length ##
roadLength = "road_km"
fieldnames = [f.name for f in arcpy.ListFields(fishnetRoadLength)]
if roadLength not in fieldnames:
    arcpy.AddField_management(fishnetRoadLength, roadLength, "DOUBLE", 18, 6)
arcpy.CalculateField_management(fishnetRoadLength,roadLength,"!shape.length@kilometers!")

print("done")
